<?php


namespace app\api\model;


use ApiOper;
use app\common\model\BaseModel;
use common;

class ArticleModel extends BaseModel
{
    protected $name = 'article';
    protected $pk = 'id';

    protected $_selectlist = [
        'city_id' => 'string',
        'id' => 'int',
        'cate_id' => 'int',
        'title' => 'string',
        'tags' => 'array',
        'content' => 'string',
        'rank' => 'int',
        'state' => 'int',
        'creator_id' => 'string',
        'create_time' => 'string',
        'creator_name' => 'string',
        'updater_id' => 'string',
        'update_time' => 'string',
        'updater_name' => 'string',
        'start_time' => 'string',
        'end_time' => 'string',
    ];

    public function scopeCity($query,$city_id)
    {
        if (empty($city_id)){
            $query->whereNull('city_id');
        }
        else{
            $query->where('city_id',$city_id)->whereOr('city_id',null);
        }
    }

    /*
     *
     * */
    function query_list_by_cate($city_id, $cate_id)
    {
        return $this->city($city_id)->where([
            'cate_id' => $cate_id,
            'state'  => 0,
        ])->order('rank')->select();
    }

    function query_list_by_tag($city_id, $tag)
    {

    }

    function query_list($city_id)
    {
        return $this->city($city_id)->order('rank')->select();
    }

    function get_content($id)
    {
        $user = $this->get($id);
        if (empty($user)){
            return null;
        }
        else{
            return $user->content;
        }
    }


    function update_article($id, $info)
    {
        $sl = $this->_selectlist;
        unset($sl['id']);


        $info = common::type_conver($info, $sl);

        ApiOper::instance()->append_updater_info($info);
        $where = [
            'id' => $id,
        ];
        //hisql::update('tb_article', $info, $where);

        self::where($where)->update($info);
    }

    function add($info)
    {
        $info = common::type_conver($info, $this->_selectlist);
        $info['state'] = 0;
        ApiOper::instance()->append_creater_info($info);

        self::create($info,true);
    }

}